package keter.lab.grpc.cloud;

import com.alibaba.fastjson.JSONObject;
import com.google.common.io.Files;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by gulx@neusoft.com on 2019/12/9.
 */
@Component
public class PerfData {
    private static final Logger logger = LoggerFactory.getLogger(PerfData.class);
    @Value("${string.length}")
    int length;
    @Value("${data.path}")
    String dataPath;
    private static String randomString;
    private static String bigJson;
    static JSONObject jsonObjectData;
    static {
        JSONObject obj1 = makeJsonObj();
        JSONObject obj2 = makeJsonObj(obj1);
        JSONObject obj3 = makeJsonObj(obj2);
        JSONObject obj4 = makeJsonObj(obj3);
        JSONObject obj5 = makeJsonObj(obj4);
        jsonObjectData = makeJsonObj(obj5);
    }

    private static JSONObject makeJsonObj(){
        JSONObject obj = new JSONObject();
        obj.put("name","haha");
        obj.put("count",1);
        for (int i = 0; i < 100; i++) {
            obj.put(RandomStringUtils.randomAlphabetic(10),RandomStringUtils.randomAlphabetic(10));
        }
        return obj;
    }
    private static JSONObject makeJsonObj(JSONObject data){
        JSONObject obj = new JSONObject();
        obj.put("name","haha");
        obj.put("count",1);
        obj.put("obj",data);
        return obj;
    }

    @Autowired
    public void init() throws IOException {
        randomString = RandomStringUtils.randomAlphabetic(length);
//        logger.info("cp:{}",new ClassPathResource("big.json").getURL());
//        java.io.File f = new ClassPathResource("big.json").getFile();
//        bigJson = Files.asCharSource(f, Charset.forName("UTF-8")).read();
        bigJson =  Files.asCharSource(new java.io.File(dataPath), Charset.forName("UTF-8")).read();
        Validate.notNull(randomString);
        Validate.notNull(bigJson);
        System.out.println("测试数据初始化完成!");
    }

    public String bigJsonString() {
        return bigJson;
    }

    public JSONObject jsonObjectData() {
        return jsonObjectData;
    }

}
